package com.jcloudsoft.springbootshardingsphere.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jcloudsoft.springbootshardingsphere.domain.Teacher;
import com.jcloudsoft.springbootshardingsphere.mapper.TeacherMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;


import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName TestController
 * @Description TestController
 * @Author oyc
 * @Date 2020/10/24 16:56
 * @Version
 */
@RestController
@RequestMapping("test")
public class TestController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * user mapper
     */
    @Resource
    private TeacherMapper teacherMapper;

    /**
     * 用户列表
     * @return
     */
    @RequestMapping
    public List<Teacher> userList() {
        logger.info("********TestController userList()");
        List<Teacher> teachers = teacherMapper.selectList(null);
        return teachers;
    }

    /**
     * 保存用户
     * @return
     */
    @PostMapping
    public Teacher save(Teacher teacher) {
        logger.info("********save Teacher");
        int insert = teacherMapper.insert(teacher);
        return teacher;
    }
    /**
     * 功能描述：分页
     *
     * @param:
     * @Return:
     * @Author: wangfei 2022/2/16 11:41
     */

    public Page<Teacher> testpage() {


        LambdaQueryWrapper<Teacher> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.ge(Teacher::getAge , 50);
        queryWrapper.orderBy(true,true,Teacher::getUserId);
        Page<Teacher> teacherPage = new Page<>(0 , 10);
        teacherMapper.selectPage(teacherPage,queryWrapper);  System.out.println("总页数： "+teacherPage.getPages());
        System.out.println("总记录数： "+teacherPage.getTotal());
        teacherPage.getRecords().forEach(System.out::println);


        return teacherPage;
    }

}